<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.initChart()
    // })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(data, model, tabName) {
      console.log(data, '11')
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          // data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月'],
          data: data.map(item => item.xAxle + '' + model),
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          name: tabName,
          axisTick: {
            show: false
          },
          min: 0,
          axisLabel: {
            formatter: tabName == '销售额' ? '{value} 元' : '{value} 单',
            // align: 'center'
          }
        }],
        series: [{
          name: tabName,
          type: 'bar',
          stack: 'vistors',
          // barWidth: '40%',
          barGap: '20%',
          barCategoryGap: '40%',
          // data: [314, 199, 200, 311,268 , 330, 156, 200, 189],
          data: data.map(item => item.yAxle),
          animationDuration,
          itemStyle: {
            color: 'rgb(49, 127, 241)',
          }
        }]
      })
    }
  }
}
</script>
